<!-- 维系知识库 -->
<template>
  <div class="app-container">
    <div class="search-and-button" :class="{ showSearch: show }">
      <el-button type="primary" icon="el-icon-plus" @click="showDialog">新增</el-button>
      <ImportFile url="/equip/knowledge/import" @getTableInfo="getTableInfo" />
      <el-button type="primary" plain :icon="!show ? 'el-icon-arrow-down' : 'el-icon-arrow-up'" @click="show = !show">查询条件</el-button>
      <el-button type="primary" icon="el-icon-search" @click="getTableInfo()">查询</el-button>
      <transition name="el-zoom-in-top">
        <div v-show="show" class="more-search">
          <el-form :model="search" label-width="80px">
            <el-row>
              <el-col :xs="24" :xl="6" :md="12">
                <el-form-item label="故障代码" prop="faultCode">
                  <el-input v-model="search.faultCode" placeholder="请输入故障代码进行搜索" clearable @keyup.enter.native="getTableInfo()" @clear="getTableInfo()" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :xl="6" :md="12">
                <el-form-item label="故障类型" prop="faultType">
                  <el-input v-model="search.faultType" placeholder="请输入故障类型进行搜索" clearable @keyup.enter.native="getTableInfo()" @clear="getTableInfo()" />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :xl="6" :md="12">
                <el-form-item label="设备" prop="keyword">
                  <el-input v-model="search.keyword" placeholder="请输入设备名称或编码进行搜索" clearable @keyup.enter.native="getTableInfo()" @clear="getTableInfo()" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </transition>
    </div>
    <el-row class="table">
      <el-table v-loading="loading" :data="tableData" ref="tableData" stripe border>
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column align="center" type="index" label="索引" width="60px" />
        <el-table-column v-for="item in fieldList" :key="item.id" :align="item.align || 'left'" :prop="item.code.split(':')[1]" :label="item.val" :min-width="item.width + 'px'" />
        <el-table-column label="操作" fixed="right" align="center" width="100px">
          <template #default="{ row }">
            <el-button-group>
              <el-button type="primary" icon="el-icon-search" @click="showDialog(row)" />
              <!-- <el-button type="danger" icon="el-icon-delete" @click="deleteRow(row)" /> -->
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <div class="table-btn-box">
        <el-button
          @click="
            () => {
              tableData.forEach(row => {
                $refs['tableData'].toggleRowSelection(row, true)
              })
            }
          "
          >选择全部</el-button
        >
        <el-button
          @click="
            () => {
              tableData.forEach(row => {
                $refs['tableData'].toggleRowSelection(row)
              })
            }
          "
          >反向选择</el-button
        >
        <el-button> 更多菜单<i class="el-icon-arrow-down el-icon--right"></i> </el-button>
      </div>
      <pagination ref="Pagination" :total="total" :current.sync="search.current" :size.sync="search.size" @pagination="getTableInfo" />
    </el-row>
    <!-- 新增编辑 -->
    <addAndModify ref="addAndModify" :eq-all-list="eqAllList" :user-all-list="userAllList" @getTableInfo="getTableInfo" />
  </div>
</template>
<script>
import { getEqListApi, getUserListApi } from '@/api/public'
import ImportFile from '@/components/ImportFile'
import Pagination from '@/components/Pagination'
import addAndModify from './components'
export default {
  components: { Pagination, ImportFile, addAndModify },
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      search: {
        current: 1,
        size: 20,
        faultCode: '',
        faultType: '',
        keyword: ''
      },
      eqAllList: [],
      userAllList: [], // 人员
      show: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getTableInfo()
    this.getEqlAll()
    this.getUserAll()

    window.onresize = () => {
      const searchBox = document.querySelector('.showSearch')
      this.changeHeight(searchBox)
    }
  },
  beforeDestroy() {
    window.onresize = null
  },
  methods: {
    /* 获取所有设备 */
    async getEqlAll() {
      const res = await getEqListApi()
      if (res?.data.code === 200) {
        this.eqAllList = res.data.data
      }
    },
    /* 获取所有人员 */
    async getUserAll() {
      const res = await getUserListApi()
      if (res?.data.code === 200) {
        this.userAllList = res.data.data
      }
    },
    /**
    /**
     * @description: height change
     * @param {*} searchBox
     * @return {*}
     */
    changeHeight(searchBox) {
      if (searchBox) {
        const clientWidth = document.documentElement.clientWidth
        if (clientWidth < 1837) {
          searchBox.style.height = '135px'
        }
        if (clientWidth < 867) {
          searchBox.style.height = '215px'
        }
        if (clientWidth > 1837) {
          searchBox.style.height = '95px'
        }
      }
    },

    /**
     * @description: page
     * @param {*} page
     * @return {*}
     */
    async getTableInfo(page = 1) {
      this.search.current = page
      this.loading = true
      const res = await this.$axios.post('/equip/knowledge/list', this.search)
      if (res?.data.code === 200) {
        this.tableData = res.data.data.list
        this.total = res.data.data.total
        setTimeout(() => {
          this.loading = false
        }, 100)
      } else {
        this.loading = false
      }
    },
    /* 新增编辑 */
    showDialog(row) {
      this.$refs['addAndModify'].showDialog(row)
    },
    /* 删除 */
    deleteRow(row) {
      this.$confirm(`确认删除数据【${row.faultCode}】？`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$axios.delete(`/equip/knowledge/remove/${row.id}`)
          if (res.data.code === 200) {
            this.$notify.success('删除成功！')
            this.getTableInfo()
          } else {
            this.$notify.error(res.data.msg)
          }
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang='scss' scoped>
.search-and-button {
  min-height: 33px;
  line-height: 30px;
  height: 30px;
  overflow: hidden;
  transition: height 0.3s;
  width: 100%;
  .el-input {
    width: 350px;
    float: right;
  }
}
.showSearch {
  height: 95px;
}
.arrow {
  text-align: center;
  cursor: pointer;
  font-size: 13px;
}

.more-search {
  margin-top: 10px;
  background-color: #f5f7fa;
  border-radius: 10px;
  .el-form {
    padding: 10px 20px;
    .el-col {
      max-width: 380px;
      min-width: 150px;
      height: 40px;
      line-height: 40px;
      .el-input {
        width: 100%;
      }
    }
  }
}
.table {
  margin-top: 20px;
}
</style>
